<?php

namespace app\api\controller;

use app\model\zs\ZsBusiness;
use app\model\zs\ZsBusinessGrassConf;
use app\model\zs\ZsMemberCoupon;
use app\mytrait\ArrTrait;
use think\Request;

class MPBusiness
{
    use ArrTrait;

    private $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 获取商户种草设置
     */
    public function getGrassConf()
    {
        $business_id = $this->request->param('business_id');

        $model_business = ZsBusiness::find($business_id);
        if (!$model_business || intval($model_business->status) === ZsBusiness::STATUS_OFF) {
            return jsonData(0, '商户不存在');
        }

        $list = $this->findGrassConfList($business_id);
        $list_conf_type = array_combine($list->column('type'), $list->all());

        $typeRels = ZsBusinessGrassConf::typeRels();

        $list_coupon = $this->getCouponList();
        $list_coupon_combine = $this->arrKeyBy($list_coupon, 'value');
        // $list_conf = collect($list_conf_type)->map(function ($item_conf) use ($typeRels, $list_coupon_combine) {
        //     $coupon_value_arr = collect($item_conf->coupon_tid_arr)->map(function ($coupon_tid) use ($list_coupon_combine) {
        //         return $list_coupon_combine[$coupon_tid]['title'] ?? '';
        //     })
        //         ->filter()
        //         ->all();
        //     return [
        //         'coupon_tid_str' => implode(',', $item_conf->coupon_tid_arr),
        //         'coupon_value' => implode(',', $coupon_value_arr),
        //         'integral' => $item_conf->integral,
        //         'red_amount' => $item_conf->red_amount,
        //     ];
        // });

        $list_type = $this->listType();

        $list_conf = collect($list_type)->map(function ($item_type) use ($list_conf_type, $list_coupon_combine) {
            $item_conf = $list_conf_type[$item_type['key']] ?? null;
            $coupon_value_arr = collect($item_conf->coupon_tid_arr ?? [])->map(function ($coupon_tid) use ($list_coupon_combine) {
                return $list_coupon_combine[$coupon_tid]['title'] ?? '';
            })
                ->filter()
                ->all();
            return [
                'type' => $item_type['key'],
                'coupon_tid_str' => implode(',', $item_conf->coupon_tid_arr ?? []),
                'coupon_value' => implode(',', $coupon_value_arr),
                'integral' => $item_conf->integral ?? '',
                'red_amount' => $item_conf->red_amount ?? '',
            ];
        });
        $list_conf = array_combine($list_conf->column('type'), $list_conf->all());

        return jsonData(1, '成功', compact('list_coupon', 'list_conf', 'list_type'));
    }

    /**
     * 保存商户种草设置
     */
    public function saveGrassConf()
    {
        $business_id = $this->request->param('business_id');

        $type_wrap = $this->request->param('type_wrap');
        $coupon_tid_arr_wrap = $this->request->param('coupon_tid_arr_wrap');
        $integral_wrap = $this->request->param('integral_wrap');
        $red_amount_wrap = $this->request->param('red_amount_wrap');

        $now = date('Y-m-d H:i:s');
        foreach (array_values($type_wrap) as $type) {
            $model_conf = $this->firstGrassConf($business_id, $type);
            if (!$model_conf) {
                $model_conf = new ZsBusinessGrassConf();
                $model_conf->type = $type;
                $model_conf->business_id = $business_id;
            }
            $model_conf->coupon_tid_arr = $coupon_tid_arr_wrap[$type];
            $model_conf->integral = $integral_wrap[$type] ?? 0;
            $model_conf->red_amount = $red_amount_wrap[$type] ?? 0;
            $model_conf->edit_at = $now;
            $model_conf->save();
        }

        return jsonData(1, '保存成功');
    }

    /**
     * 商户所有优惠券列表
     * 返回的格式要符号 weui 的 select 的需要
     */
    private function getCouponList()
    {
        $business_id = $this->request->param('business_id');

        $list_coupon = $this->findCouponList($business_id);
        $list = $list_coupon->map(function ($item_coupon) {
            return [
                // 'tid' => $item_coupon->tid,
                // 'tname' => $item_coupon->tname,
                // 'desc' => $item_coupon->desc,
                // 't_amount' => $item_coupon->t_amount,
                'title' => $item_coupon->tname,
                'value' => $item_coupon->tid,
                'checked' => false,
            ];
        });
        return $list;
    }

    /**
     * 根据 business_id 查询商户种草设置
     */
    private function findGrassConfList($business_id)
    {
        $list =  ZsBusinessGrassConf::where('business_id', $business_id)
            ->where('status', ZsBusinessGrassConf::STATUS_ON)
            ->select();

        return $list;
    }

    /**
     * 根据 business_id 和 type 查询商户种草设置
     */
    private function firstGrassConf($business_id, $type)
    {
        $model_conf = ZsBusinessGrassConf::where('business_id', $business_id)
            ->where('type', $type)
            ->where('status', ZsBusinessGrassConf::STATUS_ON)
            ->find();

        return $model_conf;
    }

    /**
     * 根据 bid 获取所有优惠券
     */
    private function findCouponList($business_id)
    {
        $list = ZsMemberCoupon::where('bid', $business_id)
            ->where('receive_status', ZsMemberCoupon::RECEIVE_STATUS_ON)
            ->where('status', ZsMemberCoupon::STATUS_PASS)
            ->order('id', 'desc')
            ->select();

        return $list;
    }

    /**
     * 根据 bid 获取所有优惠券
     */
    private function listType()
    {
        $list = [];
        $typeRels = ZsBusinessGrassConf::typeRels();
        foreach ($typeRels as $key => $name) {
            $list[] = compact('key', 'name');
        }

        return $list;
    }
}
